<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>通信历史管理</title>
<jsp:include page="../inc.jsp" flush="false"></jsp:include>
<script type="text/javascript"
        src="${pageContext.request.contextPath}/plug-in/custom/form.js"
        charset="utf-8"></script>
<script type="text/javascript">
Ext.onReady(function () {
    Ext.QuickTips.init();
    createGrid();
    grid.executeQuery();
    createLayout();
    createForm();
    form = Ext.create('communicationHistoryForm');
});
var gridEl = 'communicationHistoryGrid';
var grid, form, selModel, toolbar, searchFlds;
var win;


function createWindows(title) {
    if (win == null) {
        win = new Ext.Window({
            title: '新增',
            iconCls: 'icon-win',
            autoHeight: true,
            width: 600,
            height: 300,
            buttonAlign: 'center',
            buttons: [
                {
                    text: "发送",
                    handler: function () {
                        if (form.getForm().isValid()) {
                            form.getForm().submit({
                                url: getRootPath() + '/communicationHistoryController/save.action',
                                scope: form,
                                success: function (form, action) {
                                    win.hide();
                                    grid.getStore().reload();
                                },
                                failure: function (form, action) {
                                    if (action.result.message == undefined)
                                        Ext.Msg.alert('错误', '网络连接失败.');
                                    else
                                        Ext.Msg.alert('错误', action.result.message);
                                },
                                waitMsg: '保存数据...'
                            });
                        } else {
                            Ext.Msg.alert('提示', '表单含有无效或空数据');
                        }
                    },
                    id: "btnSave"
                },
                {
                    text: "关闭",
                    id: "btnClose",
                    handler: function () {
                        win.hide();
                    }
                }
            ],
            layout: 'fit',
            plain: false,
            closable: false,
            modal: true,
            items: [ form ]
        });
    }
    win.setTitle(title);
    win.show();
}

// 创建form表单
function createForm() {
    Ext.define('communicationHistoryForm', {
        extend: 'Ext.form.Panel',
        fields: new Ext.util.MixedCollection(),
        initComponent: function () {
            this.fields.add('id', new Ext.form.Hidden({
                name: 'id',
                dataIndex: 'id'
            }));
            this.fields.add('deleteFlag', new Ext.form.Hidden({
                name: 'deleteFlag',
                dataIndex: 'deleteFlag'
            }));
            this.fields.add('type', new Ext.form.Hidden({
                name: 'type',
                dataIndex: 'type'
            }));
            this.fields.add('receptionName', new Ext.form.TextField({
                name: 'receptionName',
                dataIndex: 'receptionName',
                fieldLabel: '收信人',
                disabled:true,
                width:190,
                value:'<%=request.getParameter("receptionName")%>',
                allowBlank: true
            }));
            this.fields.add('receptionPhoneOrEmail', new Ext.form.TextField({
                name: 'receptionPhoneOrEmail',
                dataIndex: 'receptionPhoneOrEmail',
                disabled:true,
                allowBlank: false
            }));
            this.fields.add('title', new Ext.form.TextField({
                name: 'title',
                dataIndex: 'title',
                fieldLabel: '主题',
                allowBlank: false
            }));
            this.fields.add('stencil', new Ext.form.TextField({
                name: 'stencil',
                dataIndex: 'stencil',
                fieldLabel: '模板',
                allowBlank:    true
            }));
            this.fields.add('businessKey', new Ext.form.Hidden({
                name: 'businessKey',
                dataIndex: 'businessKey',
                value:'<%=request.getParameter("businessKey")%>'
            }));
            this.fields.add('content', new Ext.form.TextArea({
                name: 'content',
                dataIndex: 'content',
                fieldLabel: '内容',
                allowBlank: false
            }));
            this.items = [ this.fields.get('id'), this.fields.get('deleteFlag'),
                    this.fields.get('type'),
                {
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {

                        hideLabel: true
                    },
                    items: [this.fields.get('receptionName'),
                    this.fields.get('receptionPhoneOrEmail')]},
                {
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {
                        hideLabel: true
                    },
                    items: [ this.fields.get('stencil'),new Ext.Button({
                        name: 'btnEntityID',
                        dataIndex: 'btnEntityID',
                        iconCls: 'searchIcon',
                        handler: function () {

                        },
                        width: 25
                    })]},
                this.fields.get('title'),
                    this.fields.get('businessKey'), this.fields.get('content') ];
            this.xtype = 'form';
            this.frame = false;
            this.border = true;
            this.split = true;
            this.autoScroll = true,
                    this.bodyPadding = 10,
                    this.bodyBorder = true;
            this.fieldDefaults = {
                labelWidth: 75,
                labelAlign: 'right',
                msgTarget: 'side'
            };
            this.defaults = {
                anchor: '100%'
            };
            this.callParent(arguments);
        },
        onRender: function () {
            this.callParent(arguments);
        },
        newDataRecord: function () {
            this.getForm().reset();
        }
    });
}


function createGrid() {
    searchFlds = new Ext.util.MixedCollection();

    searchFlds.add('businessKey', new Ext.form.Hidden({
        name : 'businessKey',
        value:'<%=request.getParameter("businessKey")%>'
    }));
    grid = Ext.create('gridView', {
        remoteSort: true,
        gridId: gridEl,
        checkboxSelectionModel: true,
        dataAutoLoad:false,
        searchFlds: searchFlds,
        entityClassName: 'com.rover.crm.entity.CommunicationHistory',
        queryUrl: 'communicationHistoryController/query.action',
        deleteUrl: 'communicationHistoryController/delete.action',
        recordPk: [ "id" ]
    });
}
// 创建layout
function createLayout() {
    toolbar = new Ext.Toolbar({
        enableOverflow: true,
        items: [
            {
                text: "发送短信",
                iconCls: 'addIcon',
                id: "tlb_phone",
                handler: function () {
                    form.newDataRecord();
                    form.getForm().findField("title").setVisible(false);
                    form.getForm().findField("receptionPhoneOrEmail").setValue('<%=request.getParameter("phone")%>');
                    createWindows("发送短信");
                }
            },
            '-',{
                text: "发送邮件",
                iconCls: 'addIcon',
                id: "tlb_email",
                handler: function () {
                    form.newDataRecord();
                    form.getForm().findField("title").setVisible(true);
                    form.getForm().findField("receptionPhoneOrEmail").setValue('<%=request.getParameter("email")%>');
                    createWindows("发送邮件");
                   
                }
            },
             "->",
            ,
            {
                text: "搜索",
                iconCls: 'searchIcon',
                handler: function () {
                    grid.load();
                }
            }
        ]
    });
    var viewport = new Ext.Viewport({
        layout: 'border',
        items: [
            {
                layout: 'fit',
                region: 'north',
                border: false,
                items: [ toolbar ]
            },
            {
                layout: 'border',
                region: 'center',
                border: false,
                items: [ grid ]
            }
        ]
    });
    viewport.doLayout();
}
</script>
</head>
<body>
</body>
</html>